﻿/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
**   * Redistributions of source code must retain the above copyright
**     notice, this list of conditions and the following disclaimer.
**   * Redistributions in binary form must reproduce the above copyright
**     notice, this list of conditions and the following disclaimer in
**     the documentation and/or other materials provided with the
**     distribution.
**   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
**     the names of its contributors may be used to endorse or promote
**     products derived from this software without specific prior written
**     permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef EXAMPLE_H
#define EXAMPLE_H

#include <QMenu>
#include <QMainWindow>
#include <QMouseEvent>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsItem>
#include "JistCommonModel.h"
#include "ImageItem.h"
class JistDocument;

//class ImageItem: public QGraphicsRectItem
//{
//public:
//    int m_iEffIndex;

//    QImage m_buffer;
//    QImage m_base_buffer;

//    ImageItem( QImage *img );
//    ImageItem();
//    QImage m_Image;
//    ~ImageItem() {}
//protected:
//    void paint( QPainter *, const QStyleOptionGraphicsItem *option, QWidget *widget );
//private:

//    QPixmap pixmap;
//};

class JistSimpleTextItem : public QGraphicsSimpleTextItem
{
public:
     void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);

};

enum ViewMode{
    DEFAULT_VIEW,
    SOURCE_IMAGE_VIEW,
    SPRITE_VIEW
};


class jistCanvas : public QGraphicsScene
{
    Q_OBJECT

    void mousePressEvent(QGraphicsSceneMouseEvent *event);
    void mouseMoveEvent(QGraphicsSceneMouseEvent *event);

public:
    QPoint GetTouchPos() {return m_TouchPos; }

private:
    QPoint m_TouchPos;

};

class FigureEditor : public QGraphicsView
{
    Q_OBJECT

public:
    QImage m_Img;
    QGraphicsRectItem *m_RectItem;
    JistSimpleTextItem  *m_TextItem;

    QRect m_SelectRect;
    JistCommonModel *m_pModel;
    jistCanvas m_canvas;
    QString m_Name;
    qreal m_ScaleX;
    qreal m_ScaleY;
    bool m_bShowGrid;

    int m_iAnimationIndex;
    int m_iPreAnimationIndex;
    int m_iFrameIndex;
    bool m_bLoopAni;
    bool m_bAniPlaying;
    bool m_bReverse;
    // 신편집뷰
    bool m_bMovable;
    bool m_bDragMovable;

    ImageItem *m_selectEditingItem;
    QPointF m_selectedStartPos;
    QPointF m_startPos;

    QVector< QVector< QList< JistCommonData > > > &m_CellData;
    JistDocument *m_pDocument;

    bool m_IsAdditive;
    QImage *m_buffImg;
    QImage m_EnermyImage;
    QGraphicsLineItem *m_LineX2;
    QGraphicsLineItem *m_LineRange;
    QGraphicsPixmapItem * m_enermyPix;
    QPointF m_enermyPixPos;
    bool m_bShowEnermy;
    QList<QGraphicsItem *> m_Selectlist;

    bool m_bGroupBand;
    bool m_bEmptySprite;

    ViewMode m_eViewMode;
    QGraphicsLineItem *m_AnchorVLine;
    QGraphicsLineItem *m_AnchorHLine;
public:
    FigureEditor(QVector< QVector< QList< JistCommonData > > > *cellData = NULL, QWidget* parent=0, const char* name=0, Qt::WindowFlags f=0);
    void setViewMode(ViewMode mode);
    void clear();
    void mousePressEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
    void mouseReleaseEvent(QMouseEvent *event);
    void mouseDoubleClickEvent(QMouseEvent *event);
    void wheelEvent(QWheelEvent *event);

//    void dragEnterEvent ( QDragEnterEvent * event );
//    void dragLeaveEvent ( QDragLeaveEvent * event );
//    void dragMoveEvent ( QDragMoveEvent * event );
    void keyPressEvent ( QKeyEvent * event );
    void keyReleaseEvent( QKeyEvent * event );

    //void paintEvent(QPaintEvent *event);
    QRect computeFillRegion(const QPoint &fillOrigin) const;
    bool isDrawable(const int x, const int y)const;
    void setDocument(JistDocument *doc);
    void setModel(JistCommonModel *model);
    void setAdditive(bool isAdditive);
    QGraphicsPixmapItem* setImage( QImage img);
    QGraphicsPixmapItem* setImage();
    int NextPot(int n);
    void creatFakeBMP();
    void addImage( QImage img);
    void setName(QString s);
    QString getName();
    void setMovableTouch(bool movable);
    void setScale(qreal sx, qreal sy);
    void setRect(QRect r);
    void changeEnermy(int monType);
    void addItem(ImageItem* item);

    void setPlayState(bool isPlaying);
    void resetAni();

    void finishMoving(const QPointF &pos);
    void updateAll();
    void drawBackground( QPainter * painter, const QRectF & rect );
    void render( QPainter * painter, const QRectF & target = QRectF(), const QRect & source = QRect(), Qt::AspectRatioMode aspectRatioMode = Qt::KeepAspectRatio );
    void contextMenuEvent(QContextMenuEvent *event);
    QPoint GetModifedPos();
    QPoint GetAnchorPos();
    void SetRectItem();
public slots:
    void AddSprite(bool isEmptySprite = true);
    void animate();
    void exchangeSprite();
    void clearColision();
protected:
    //void paintEvent ( QPaintEvent * event );
signals:
    void status(const QString&);
    void changeSelItemPos(QPointF pos, int z, QRect r);
};


/*
class Main : public QMainWindow {
    Q_OBJECT

public:
    Main(QGraphicsScene&, QWidget* parent=0, Qt::WindowFlags f=0);
    ~Main();

public slots:
    void help();

private slots:
    void aboutQt();
    void newView();
    void clear();
    void init();

    void addSprite();
    void addCircle();
    void addHexagon();
    void addPolygon();
    void addSpline();
    void addText();
    void addLine();
    void addRectangle();
    void addMesh();
    void addLogo();
    void addButterfly();

    void enlarge();
    void shrink();
    void rotateClockwise();
    void rotateCounterClockwise();
    void zoomIn();
    void zoomOut();
    void mirror();
    void moveL();
    void moveR();
    void moveU();
    void moveD();

    void print();

private:
    QGraphicsScene& canvas;
    FigureEditor *editor;

    QMenu* options;
#if !defined(Q_OS_SYMBIAN)
    QPrinter* printer;
#endif
};
*/
#include "JistDocument.h"

#endif
